package com.jk11a.shangdaast.notice;

import java.util.List;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.NoticeDao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class GetMyNotice extends Activity{

	private static final int MSG_SUCCESS = 0; //获取成功
	private static final int MSG_FAILURE = 1; //获取失败
	private Thread mynoticeThread;
	private SharedPreferences sp=null;
	private String FILE = "saveUserNamePwd";
 @Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.notice_mynotice);
	sp = this.getSharedPreferences(FILE, Context.MODE_PRIVATE);
	if(mynoticeThread==null){
		mynoticeThread=new Thread(runnable);
		mynoticeThread.start();
	}
	else {
		Toast.makeText(getApplication(), "mynoticeThread thread 已启动",  Toast.LENGTH_LONG).show();
	}
}
 Runnable runnable=new Runnable() {
		// 重写run()方法，此方法在新的线程中运行
		@Override
		public void run() {
			// TODO Auto-generated method stub
			NoticeDao noticeDao=new NoticeDao();
			List<NoticeBean> notices = null;
			try {
				notices = noticeDao.getmynotice(sp.getInt("id", 0));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 获取 成功，向UI线程发送MSG_SUCCESS标识和books对象
			if(notices.size()!=0){
			mHandler.obtainMessage(MSG_SUCCESS,notices).sendToTarget();
			}
			else{
				// 获取 失败，向UI线程发送MSG_FAILURE标识
				 mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				 return;
			}
		}
	};
	private Handler mHandler=new Handler(){
		// 重写handleMessage()方法，此方法在UI线程运行
		@Override
		public void handleMessage(Message msg){
			switch (msg.what) {
			case MSG_SUCCESS:
				List<NoticeBean> notices=(List<NoticeBean>)msg.obj;
				popnoticelist	(notices);  
				break;

			case MSG_FAILURE:
				Toast.makeText(getApplication(), "获取已发布通知失败",  Toast.LENGTH_LONG).show();
				break;
			}
		}
	};
	private void popnoticelist(List<NoticeBean> notices){
		int j = 1;
		int i = 0;
     for (i = 0; i < notices.size(); i++) {
		RelativeLayout rLayout = new RelativeLayout(this);
		if(i==0){
			rLayout.setBackgroundResource(R.drawable.preference_first_item);
			rLayout.setTop(14);
		}else{
			rLayout.setBackgroundResource(R.drawable.preference_item);
		}
		rLayout.setClickable(true);
		rLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.setClass(GetMyNotice.this, Showonemynotice.class);
				startActivity(intent);
				finish();
			}
		});
		rLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
		
		rLayout.setGravity(Gravity.CENTER);
		TextView tv = new TextView(this);
	
		tv.setText(notices.get(i).getTitle()+"			"+notices.get(i).getPublishtime());
		tv.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv.setTextColor(Color.rgb(0, 0, 0));
		rLayout.addView(tv);
		((LinearLayout) this.findViewById(R.id.mynoticelist)).addView(rLayout);
		
	}

}

}
